package controller;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

import data.Data;

@SuppressWarnings("serial")
public class EditBlogServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
				
		String blog = request.getParameter("blog");
		
		HttpSession session = request.getSession();
		DBObject dbObject = (DBObject) session.getAttribute("users");
		if(dbObject==null)
		{
			try {
				response.sendRedirect("/");
				return;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		DBCollection collection = Data.data("blogCraw");
		DBCursor cursor=collection.find(new BasicDBObject("username",dbObject.get("username")));
		request.setAttribute("cursor", cursor);
		
		String[] listError = new String[7];
		request.setAttribute("listError", listError);
		
		
		DBObject object = collection.findOne(new BasicDBObject("username",dbObject.get("username")).append("blogurl", blog));
		request.setAttribute("object", object);
		
		try {
			request.getRequestDispatcher("/edit_blog.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException {

		String blog = request.getParameter("blog");
		HttpSession session = request.getSession();
		DBObject dbObject = (DBObject) session.getAttribute("users");
		if(session.getAttribute("users")==null)
		{
			try {
				response.sendRedirect("/");
				return;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		DBCollection collection = Data.data("blogCraw");
		DBCursor cursor=collection.find(new BasicDBObject("username",dbObject.get("username")));
		request.setAttribute("cursor", cursor);
		
		String type = request.getParameter("type");
		String[] listError = new String[7];
		if(type!=null&&type.equals("wordpress"))
		{
			String blogurl = request.getParameter("blogurl");
			String usernameblog = request.getParameter("usernameblog");
			String passwordblog = request.getParameter("passwordblog");
			
			request.setAttribute("blogurl", blogurl);
			request.setAttribute("usernameblog", usernameblog);
			
			if(blogurl!=null&&blogurl.equals(""))
				listError[0]="Blog's url is not empty !";
			if(blogurl!=null&&blogurl.equals(""))
				listError[1]="Blog's username is not empty !";
			if(blogurl!=null&&blogurl.equals(""))
				listError[2]="Blog's password is not empty !";
			
			if(listError[0]==null&&listError[1]==null&&listError[2]==null)
			{
				
				blogurl = Utils.fomatSitemap(blogurl);
				DBObject dbObject1 = collection.findOne(new BasicDBObject("blogurl",blog).append("username", dbObject.get("username")));
				if(dbObject1!=null)
				{
					DBCollection coll = Data.data("urlCraw");
					DBCursor cursor2 = coll.find(new BasicDBObject("blogurl",blog).append("username", dbObject.get("username")));
					if(cursor2.size()>0)
					{
						while (cursor2.hasNext()) {
							DBObject dbObject2 = (DBObject) cursor2.next();
							dbObject2.put("blogurl", blogurl);
							coll.save(dbObject2);
						}
					}
	
					dbObject1.put("username", dbObject.get("username"));
					dbObject1.put("usernameblog", usernameblog);
					dbObject1.put("passwordblog", passwordblog);
					dbObject1.put("blogurl", blogurl);
					dbObject1.put("type", type);
					
					collection.save(dbObject1);
					request.setAttribute("success", "yes");
				} 
			}
		}
		if(type!=null&&type.equals("blogger"))
		{
			String blogurl = request.getParameter("blogurl");
			String email = request.getParameter("email");
			String passwordemail = request.getParameter("passwordemail");
			String blogid = request.getParameter("blogid");
			
			request.setAttribute("blogurl", blogurl);
			request.setAttribute("email", email);
			request.setAttribute("blogid", blogid);
			
			if(blogurl!=null&&blogurl.equals(""))
				listError[3]="Blog's url is not empty !";
			if(blogid!=null&&blogid.equals(""))
				listError[4]="Blogid is not empty !";
			if(email!=null&&email.equals(""))
				listError[5]="Email is not empty !";
			if(passwordemail!=null&&passwordemail.equals(""))
				listError[6]="Password is not empty !";
			
			if(listError[3]==null&&listError[4]==null&&listError[5]==null&&listError[6]==null)
			{
		
				blogurl = Utils.fomatSitemap(blogurl);
				DBObject dbObject1 = collection.findOne(new BasicDBObject("blogurl",blog).append("username", dbObject.get("username")));
				if(dbObject1!=null)
				{

					DBCollection coll = Data.data("urlCraw");
					DBCursor cursor2 = coll.find(new BasicDBObject("blogurl",blog).append("username", dbObject.get("username")));
					if(cursor2.size()>0)
					{
						while (cursor2.hasNext()) {
							DBObject dbObject2 = (DBObject) cursor2.next();
							dbObject2.put("blogurl", blogurl);
							coll.save(dbObject2);
						}
					}
					
					dbObject1.put("username", dbObject.get("username"));
					dbObject1.put("email", email);
					dbObject1.put("passwordemail", passwordemail);
					dbObject1.put("blogurl", blogurl);
					dbObject1.put("blogid", blogid);
					dbObject1.put("type", type);
					
					collection.save(dbObject1);
					request.setAttribute("success", "yes");
				}
			}
		}
		request.setAttribute("listError", listError);
		request.setAttribute("type", type);
		try {
			request.getRequestDispatcher("/edit_blog.jsp").forward(
					request, response);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
